package com.leyou.search.listener;

import com.leyou.search.service.GoodsService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.leyou.common.constants.MQConstants.Exchange.ITEM_EXCHANGE_NAME;
import static com.leyou.common.constants.MQConstants.RoutingKey.ITEM_DOWN_KEY;
import static com.leyou.common.constants.MQConstants.RoutingKey.ITEM_UP_KEY;

/**
 * @author syf
 * @version 1.0
 * @date 2019/08/11 18:02
 * @Description TODO:监听器
 */
@Component
public class ItemSearchListener {

    @Autowired
    private GoodsService goodsService;

    //上架
    @RabbitListener(bindings=@QueueBinding(
            value = @Queue(value = ITEM_UP_KEY, durable = "true"), //绑定的名称,是否持久化
            exchange =@Exchange(value = ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),//交换机,指定类型
            key = ITEM_UP_KEY //消息模式
    ))
    public void itemUp(Long id){
        goodsService.AddItemToIndex(id);
    }

    //下架
    @RabbitListener(bindings=@QueueBinding(
            value = @Queue(value = ITEM_DOWN_KEY, durable = "true"), //绑定的名称,是否持久化
            exchange =@Exchange(value = ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),//交换机,指定类型
            key = ITEM_DOWN_KEY //消息模式
    ))
    public void itemDown(Long id){
        goodsService.DeleteItemToIndex(id);
    }
}
